#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ComfyUI HuggingFace下载插件
"""
import sys
from pathlib import Path

# 导入统一的环境配置
from public.env_setup import setup_environment, ENV_CONFIG, DOWNMODELS_DIR

# 使用统一的环境配置
ENV_CONFIG = setup_environment()

from public.download_utils import download_file_with_hf

class HFDownloader:
    """HuggingFace下载插件类"""
    order = 4

    def __init__(self):
        # 延迟获取环境配置
        self.env_config = ENV_CONFIG
        self.workspace_path = self.env_config['PROJECT_ROOT']
        self.default_save_path = DOWNMODELS_DIR
        self.default_save_path.mkdir(exist_ok=True)

    def start(self) -> bool:
        print("\n■■■ HuggingFace 下载器（ComfyUI插件版） ■■■")
        print(f"默认保存目录: {self.default_save_path}")
        print("0. 退出")
        print("1. 下载文件")
        choice = input("请选择功能 (0-1): ").strip()
        if choice == "0" or choice.lower() == "q":
            print("已退出插件。") 
            return False
        while True:
            print("\n==============================")
            url = input("请输入文件或文件夹URL（q退出）: ").strip()
            if url.lower() == 'q':
                print("再见！")
                break
            if not url:
                print("URL不能为空！")
                continue
            filename = input("请输入文件名（文件夹直接回车）: ").strip()
            save_path = input(f"请输入保存目录（留空默认: {self.default_save_path}）: ").strip()
            if not save_path:
                save_path = str(self.default_save_path)
            print(f"\n[下载信息]\nURL: {url}\nFileName: {filename}\n保存到: {save_path}")
            success, result = download_file_with_hf(url=url, filename=filename, save_path=save_path)
            if success:
                print(f"[成功] 已下载到: {result}")
            else:
                print(f"[失败] {result}")
        return True

    @staticmethod
    def Launch(self):
        downloader = HFDownloader()
        return downloader.start()

def main():
    downloader = HFDownloader()
    downloader.start()

if __name__ == "__main__":
    main() 